
#include <stdio.h>
#include <math.h>
#include "Cube.h"

	//constructors

	Cube::Cube()
	{

	}
	Cube::Cube(double v0, double v1, double v2, double v3, double v4, double v5, double v6,  double v7, int ijk[3])
	{
		_vertex[0] = v0;
		_vertex[1] = v1;
		_vertex[2] = v2;
		_vertex[3] = v3;
		_vertex[4] = v4;
		_vertex[5] = v5;
		_vertex[6] = v6;
		_vertex[7] = v7;

		_initialVertex[0] = ijk[0];
		_initialVertex[1] = ijk[1];
		_initialVertex[2] = ijk[2];
	}

	Cube::Cube(double v0, double v1, double v2, double v3, double v4, double v5, double v6,  double v7)
	{
		_vertex[0] = v0;
		_vertex[1] = v1;
		_vertex[2] = v2;
		_vertex[3] = v3;
		_vertex[4] = v4;
		_vertex[5] = v5;
		_vertex[6] = v6;
		_vertex[7] = v7;

	}

	void Cube::generateCube(int ijk[3],  vtkStructuredPoints *vol_v16 , vtkFloatArray *v16_datos)
	{
		int tmpIjk[3];

	    setVertex(0, *(v16_datos->GetTuple(vol_v16->ComputePointId(ijk))));
	    for(int i = 0; i < 3; i++)
	    	tmpIjk[i] = ijk[i];

	    tmpIjk[0]++;

	    setVertex(1, *(v16_datos->GetTuple(vol_v16->ComputePointId(tmpIjk))));

	    tmpIjk[1]++;

	    setVertex(2, *(v16_datos->GetTuple(vol_v16->ComputePointId(tmpIjk))));

	    tmpIjk[0]--;

	    setVertex(3, *(v16_datos->GetTuple(vol_v16->ComputePointId(tmpIjk))));

	    for(int i = 0; i < 3; i++)
	    	tmpIjk[i] = ijk[i];

	    tmpIjk[2]++;

	    setVertex(4, *(v16_datos->GetTuple(vol_v16->ComputePointId(tmpIjk))));

	    tmpIjk[0]++;

	    setVertex(5, *(v16_datos->GetTuple(vol_v16->ComputePointId(tmpIjk))));

	    tmpIjk[1]++;

	    setVertex(6, *(v16_datos->GetTuple(vol_v16->ComputePointId(tmpIjk))));

	    tmpIjk[0]--;

	    setVertex(7, *(v16_datos->GetTuple(vol_v16->ComputePointId(tmpIjk))));

	    for(int i = 0; i < 3; i ++)
	    {
	    	_initialVertex[i] = ijk[i];
	    }
	}

	void Cube::calculateIndex(double umbral)
	{
		for(int i = 0; i < 8; i ++)
		{
			(_vertex[i] > umbral)?_indexCube.setIndex(i, true):_indexCube.setIndex(i,false);
		}
	}

	void Cube::setVertex(int i, double densidad)
	{
		_vertex[i] = densidad;
	}

	double Cube::getVertex(int i)
	{
		return _vertex[i];
	}

	Index Cube::getIndex(){
		return _indexCube;
	}

	void Cube::setIndex(Index & newIndex){
		_indexCube =  newIndex;
	}

	Cube Cube::operator=(Cube newCube)
	{
		for(int i = 0; i < 8; i++)
		{
			_vertex[i] = newCube.getVertex(i);
			_indexCube.setIndex(i, newCube.getIndex().getValue(i));
		}
		int * initVertex;
		initVertex = newCube.getInitialVertex();

		for(int i = 0; i < 3; i++)
			_initialVertex[i] = initVertex[i];

		return *this;
	}

	Cube Cube::rotate(int rotx, int roty, int rotz)
	{

		Cube tmpCube (_vertex[0],_vertex[1],_vertex[2],_vertex[3],_vertex[4],_vertex[5],_vertex[6],_vertex[7],_initialVertex);
		tmpCube.setIndex(_indexCube);
		Cube rotatedCube (_vertex[0],_vertex[1],_vertex[2],_vertex[3],_vertex[4],_vertex[5],_vertex[6],_vertex[7],_initialVertex);
		rotatedCube.setIndex(_indexCube);

		for(int i = 0; i < rotx; i++)
		{
			rotatedCube.setVertex(0, tmpCube.getVertex(3));
			rotatedCube.setVertex(1, tmpCube.getVertex(2));
			rotatedCube.setVertex(2, tmpCube.getVertex(6));
			rotatedCube.setVertex(3, tmpCube.getVertex(7));
			rotatedCube.setVertex(4,  tmpCube.getVertex(0));
			rotatedCube.setVertex(5, tmpCube.getVertex(1));
			rotatedCube.setVertex(6, tmpCube.getVertex(5));
			rotatedCube.setVertex(7, tmpCube.getVertex(4));

			tmpCube = rotatedCube;
		}
		for(int i = 0; i < roty; i++)
		{
			rotatedCube.setVertex(0, tmpCube.getVertex(1));
			rotatedCube.setVertex(1, tmpCube.getVertex(5));
			rotatedCube.setVertex(2, tmpCube.getVertex(6));
			rotatedCube.setVertex(3, tmpCube.getVertex(2));
			rotatedCube.setVertex(4, tmpCube.getVertex(0));
			rotatedCube.setVertex(5, tmpCube.getVertex(4));
			rotatedCube.setVertex(6, tmpCube.getVertex(7));
			rotatedCube.setVertex(7, tmpCube.getVertex(3));

			tmpCube = rotatedCube;

		}
		for(int i = 0; i < rotz; i++)
		{
			rotatedCube.setVertex(0, tmpCube.getVertex(3));
			rotatedCube.setVertex(1,tmpCube.getVertex(0));
			rotatedCube.setVertex(2, tmpCube.getVertex(1));
			rotatedCube.setVertex(3, tmpCube.getVertex(2));
			rotatedCube.setVertex(4, tmpCube.getVertex(7));
			rotatedCube.setVertex(5, tmpCube.getVertex(4));
			rotatedCube.setVertex(6, tmpCube.getVertex(5));
			rotatedCube.setVertex(7, tmpCube.getVertex(6));

			tmpCube = rotatedCube;

		}
		return rotatedCube;
	}

	int * Cube::getInitialVertex()
	{
		return _initialVertex;
	}
